package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Setmeal;
import com.xrit.sunnyday.service.SetmealService;
import com.xrit.sunnyday.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Reference
    private SetmealService setmealService;

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        //1.获取图片名 xx.jpg
        String originalFilename = imgFile.getOriginalFilename();
        //2.获取图片名后缀名 jpg
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //3.生成随即名
        String fileName =  UUID.randomUUID()+extension;
        //4.调用七牛
        try{
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),fileName);
            //返回给成功的回调
            Map<String,String> map = new HashMap<String,String>();
            map.put("imgName",fileName);
            map.put("domain",QiNiuUtils.DOMAIN);
            return new Result(true,"成功",map);
        }catch (IOException e){
            e.printStackTrace();
        }
        return new Result(false,"失败");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        //1.添加套餐 、以及套餐和检查组的关系
        setmealService.add(setmeal, checkgroupIds);
        //2.响应结果
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        // 调用服务分页查询
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    /**
     * 通过id查询套餐信息
     */
    @GetMapping("/findById")
    public Result findById(int id){
        //1.根据id查询对应的套餐
        Setmeal setmeal = setmealService.findById(id);
        //2.思考：前端要回显图片需要全路径,封装到map中，解决图片路径问题
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //2.1将查询到的对应套餐返回，其中包含了img的图片名称
        resultMap.put("setmeal", setmeal); // formData
        //2.2返回我们七牛云的外连接域名(指向我们的存储空间)
        resultMap.put("domain", QiNiuUtils.DOMAIN); // domain
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,resultMap);
    }
    /**
     * 通过id查询选中的检查组ids
     */
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(int id){
        List<Integer> checkgroupIds = setmealService.findCheckgroupIdsBySetmealId(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
    }
    /**
     * 修改套餐
     */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        // 调用业务服务修改
        setmealService.update(setmeal, checkgroupIds);
        // 响应结果
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    // 删除套餐
    @RequestMapping(value = "/delete")
    public Result delete(Integer id){
        setmealService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
